<?php
namespace app\controllers;

use app\common\corelib\CacheHelper;
use app\logic\CommonLogic;
use app\logic\ShopLogic;
use app\logic\UserLogic;
use app\models\Region;
use Yii;


class ShopController extends BaseController
{

	public function actionIndex()
	{
            echo "index";
	}
    /**
     * 获取商店信息
     * @apiDescription   by Jim
     * @api              {post} /shop/get-shop 获取商店信息
     * @apiName          get-shop
     * @apiVersion       1.0.0
     * @apiParam {int} shop_id 商店id
     * @apiGroup         Shop
     * @apiSampleRequest /shop/get-shop?flagsign=1
     * @apiSuccessExample {json} 获取成功
     * {
    `shop_id` int(11) NOT NULL AUTO_INCREMENT COMMENT '商家id',
    `shop_no` varchar(255) NOT NULL COMMENT '商家编号',
    `zone_id` int(11) unsigned NOT NULL COMMENT '地址id',
    `address` varchar(255) NOT NULL COMMENT '店铺地址',
    `shop_name` varchar(255) NOT NULL COMMENT '商家名称',
    `shop_parent_name` varchar(255) NOT NULL COMMENT '店铺名称',
    `longitude` int(3) unsigned NOT NULL COMMENT '经度',
    `latitude` int(3) unsigned NOT NULL COMMENT '纬度',
    `shop_pic` varchar(255) NOT NULL COMMENT '商家图片',
    `shop_pics` text NOT NULL COMMENT '商家图片集',
    `description` text NOT NULL COMMENT '商家介绍',
    `status` tinyint(1) unsigned NOT NULL DEFAULT '0' COMMENT '状态：默认0无效，1有效',
    `owner_user_id` int(11) unsigned NOT NULL COMMENT '店主id,取tbl_user表的id',
    `owner_user_name` varchar(255) NOT NULL,
    `contract_start_time` datetime DEFAULT NULL COMMENT '合同开始时间',
    `contract_end_time` datetime DEFAULT NULL COMMENT '合同结束时间',
    `contract_create_time` datetime DEFAULT NULL COMMENT '合同创建时间',
    `city_id` varchar(10) NOT NULL COMMENT '城市id',
    `district_id` varchar(10) NOT NULL COMMENT '区id',
    `street_id` varchar(10) NOT NULL COMMENT '街道id',
     * phone 当值店长电话
     * manager_name 当值店长名
      }
     * @apiError         none
     */
    public function actionGetShop()
    {
        $shop_id = $this->getQueryparams('shop_id',0);
        $shopLogic = new ShopLogic();
        $shop = $shopLogic->getShop($shop_id);
        $this->response($shop);
    }

    /**
     * 登录后获取自己的商店信息
     * @apiDescription   by Jim
     * @api              {post} /shop/get-shop-by-myself 登录后获取自己的商店信息
     * @apiName          get-shop-by-myself
     * @apiVersion       1.0.0
     * @apiParam {int} shop_id 商店id（店主可选填）
     * @apiParam {string} access_token
     * @apiGroup         Shop
     * @apiSampleRequest /shop/get-shop-by-myself?flagsign=1
     * @apiSuccessExample {json} 获取成功
     * {
    `shop_id` int(11) NOT NULL AUTO_INCREMENT COMMENT '商家id',
    `shop_no` varchar(255) NOT NULL COMMENT '商家编号',
    `zone_id` int(11) unsigned NOT NULL COMMENT '地址id',
    `address` varchar(255) NOT NULL COMMENT '店铺地址',
    `shop_name` varchar(255) NOT NULL COMMENT '商家名称',
    `shop_parent_name` varchar(255) NOT NULL COMMENT '店铺名称',
    `longitude` int(3) unsigned NOT NULL COMMENT '经度',
    `latitude` int(3) unsigned NOT NULL COMMENT '纬度',
    `shop_pic` varchar(255) NOT NULL COMMENT '商家图片',
    `shop_pics` text NOT NULL COMMENT '商家图片集',
    `description` text NOT NULL COMMENT '商家介绍',
    `status` tinyint(1) unsigned NOT NULL DEFAULT '0' COMMENT '状态：默认0无效，1有效',
    `owner_user_id` int(11) unsigned NOT NULL COMMENT '店主id,取tbl_user表的id',
    `owner_user_name` varchar(255) NOT NULL,
    `contract_start_time` datetime DEFAULT NULL COMMENT '合同开始时间',
    `contract_end_time` datetime DEFAULT NULL COMMENT '合同结束时间',
    `contract_create_time` datetime DEFAULT NULL COMMENT '合同创建时间',
    `city_id` varchar(10) NOT NULL COMMENT '城市id',
    `district_id` varchar(10) NOT NULL COMMENT '区id',
    `street_id` varchar(10) NOT NULL COMMENT '街道id',
     * name 店长或店长的名称
     * role 角色 店长或者店主
    }
     * @apiError         none
     */
    public function actionGetShopByMyself()
    {
        $shop_id = $this->getQueryparams('shop_id',0);
        $userLogic = new UserLogic();
        $user = $userLogic->getUser($this->user_id);
        if($user->type==0)
            $this->error('300002','用户账户不可查看');
        if($user->type==1){
            $shopLogic = new ShopLogic();
            $shop = $shopLogic->getShopByOwner($shop_id);
            $shop['name'] = $shop['owner_user_name'];
            $shop['role'] = "店主";
            $shop['manager_code_pic'] = '';
        }else{
            $shopLogic = new ShopLogic();
            $shop = $shopLogic->getShopByManager($this->user_id);
            $shop['name'] = $shop['manager_name'];
            $shop['role'] = "店长";
            $manager = $userLogic->getManager($this->user_id);
            $shop['on_work'] = $manager['on_work'];
            $shop['manager_code_pic'] = $manager['manager_code_pic'];
        }

        $this->response($shop);
    }
    /**
     * 店主获取商店列表
     * @apiDescription   by Jim
     * @api              {post} /shop/get-shop-list-by-owner 店主获取商店列表
     * @apiName          get-shop-list-by-owner
     * @apiVersion       1.0.0
     * @apiParam {string} access_token
     * @apiGroup         Shop
     * @apiSampleRequest /shop/get-shop-list-by-owner?flagsign=1
     * @apiSuccessExample {json} 获取成功
     * [{
    `shop_id` int(11) NOT NULL AUTO_INCREMENT COMMENT '商家id',
    `shop_no` varchar(255) NOT NULL COMMENT '商家编号',
    `zone_id` int(11) unsigned NOT NULL COMMENT '地址id',
    `address` varchar(255) NOT NULL COMMENT '店铺地址',
    `shop_name` varchar(255) NOT NULL COMMENT '商家名称',
    `shop_parent_name` varchar(255) NOT NULL COMMENT '店铺名称',
    `longitude` int(3) unsigned NOT NULL COMMENT '经度',
    `latitude` int(3) unsigned NOT NULL COMMENT '纬度',
    `shop_pic` varchar(255) NOT NULL COMMENT '商家图片',
    `shop_pics` text NOT NULL COMMENT '商家图片集',
    `description` text NOT NULL COMMENT '商家介绍',
    `status` tinyint(1) unsigned NOT NULL DEFAULT '0' COMMENT '状态：默认0无效，1有效',
    `owner_user_id` int(11) unsigned NOT NULL COMMENT '店主id,取tbl_user表的id',
    `owner_user_name` varchar(255) NOT NULL,
    `contract_start_time` datetime DEFAULT NULL COMMENT '合同开始时间',
    `contract_end_time` datetime DEFAULT NULL COMMENT '合同结束时间',
    `contract_create_time` datetime DEFAULT NULL COMMENT '合同创建时间',
    `city_id` varchar(10) NOT NULL COMMENT '城市id',
    `district_id` varchar(10) NOT NULL COMMENT '区id',
    `street_id` varchar(10) NOT NULL COMMENT '街道id',
     * `city` 城市
     * `district` 区
     * `street` 街道
    }]
     * @apiError         none
     */
    public function actionGetShopListByOwner(){
        $shopLogic = new ShopLogic();
        $shopList = $shopLogic->getShopListByOwner($this->user_id);
        $this->response($shopList);
    }
    /**
     * 人员名单
     * @apiDescription   by Jim
     * @api              {post} /shop/get-manager-list 人员名单
     * @apiName          get-manager-list
     * @apiVersion       1.0.0
     * @apiParam {int} shop_id 商店id
     * @apiParam {string} access_token
     * @apiGroup         Shop
     * @apiSampleRequest /shop/get-manager-list?flagsign=1
     * @apiSuccessExample {json} 获取成功
     *
     * {"owner": {
    "owner_user_name": "李主任"
    },
    "managers": [
    {
    "manager_id": "1",
    "user_id": "1",
    "shop_id": "1",
    "contract_start_time": "2018-04-25 00:15:47",
    "contract_end_time": "2019-04-01 00:15:50",
    "contract_create_time": "2018-04-25 00:16:05",
    "on_work": "1",
    "manager_name": "周总",
    "phone": "18664699093"
    }
    ]}只有两种角色，店主owner只有一个 店长managers
     * @apiError         none
     */
    public function actionGetManagerList(){
        $shop_id = $this->getQueryparams('shop_id',0);
        $shopLogic = new ShopLogic();
        $userLogic = new UserLogic();
        $managers = $shopLogic->getManagerList($shop_id);
        $shop = $shopLogic->getShop($shop_id);
        $owner['owner_user_name'] = $shop['owner_user_name'];
        $owner['name'] = $owner['owner_user_name'];
        $owner['job'] = "店主";
        $ownertemp = $userLogic->getUser($shop['owner_user_id']);
        $owner['phone'] = $ownertemp->phone;
        $list = [];
        $list[0] = $owner;
        foreach ($managers AS $key => &$value){
            $value['name'] = $value['manager_name'];
            $value['job'] = "店长";
            $list[] = $value;
        }
        $this->response($list);
    }






}
